package com.yuan.web.action.index;

import java.sql.SQLException;

import jxl.common.Logger;

import com.yuan.common.action.BaseAction;
import com.yuan.common.inputcheck.InputCheck;
import com.yuan.common.message.InformationMessage;
import com.yuan.service.subscribe.SubscribeService;
import com.yuan.subscribe.Subscribe;
import com.yuan.web.action.lottery.LotteryAction;

/**
 * 首页的action
 * 
 * @author Beni
 * 
 */
public class IndexAction extends BaseAction {

	private static final Logger logger = Logger.getLogger(LotteryAction.class);

	private static final long serialVersionUID = 3247642302011541690L;

	private Subscribe subscribe;

	private SubscribeService subscribeService;

	public String index() throws Exception {
		return "indexPage";
	}

	/**
	 * 用户订阅邮件
	 * 
	 * @return
	 */
	public String save() {
		try {

			if (subscribe == null || subscribe.getEmail() == null
					|| !InputCheck.checkMail(subscribe.getEmail())) {
				this.setInformationMessage(new InformationMessage("WAR003"));
				return "indexPage";
			}

			subscribeService.save(subscribe);
			this.setInformationMessage(new InformationMessage("INF002"));

		} catch (SQLException ex) {
			logger.error("用户己预订", ex);
			this.setInformationMessage(new InformationMessage("ERR002"));
			return "indexPage";
		} catch (Exception ex) {
			logger.error("用户订阅失败", ex);
			this.setInformationMessage(new InformationMessage("ERR001"));
			return "indexPage";
		}

		return "indexPage";
	}

	public SubscribeService getSubscribeService() {
		return subscribeService;
	}

	public void setSubscribeService(SubscribeService subscribeService) {
		this.subscribeService = subscribeService;
	}

	public Subscribe getSubscribe() {
		return subscribe;
	}

	public void setSubscribe(Subscribe subscribe) {
		this.subscribe = subscribe;
	}

}
